package ee.greaton.siteManager.GUI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import java.awt.GridBagLayout;

import javax.swing.JComboBox;

import ee.greaton.siteManager.DB.SQLiteConnection;
import ee.greaton.siteManager.model.Site;

import java.awt.GridBagConstraints;
import java.util.ArrayList;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;

import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import javax.swing.JScrollPane;

public class ManageSitesGUI extends JFrame implements ItemListener, ActionListener {

	private JPanel contentPane;
	private JComboBox comboBox;
	private JButton btnInstalliCms;
	private JButton btnValiInstallikaust;
	private JButton btnMuuda;
	private Object[] arr;
	
	private Site selectedSite;
	private int selectedIndex = 0;
	private JScrollPane scrollPane;
	private JComboBox comboBox_1;
	private JButton btnInstalliTemplate;
	private JButton btnInstalliMoodulid;
	private JButton btnUuendaMoodulid;

	/**
	 * Create the frame.
	 */
	public ManageSitesGUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		SQLiteConnection sqlite = SQLiteConnection.getInstance();
		arr = sqlite.getAllSites().toArray();
		if(arr.length > 0){
			selectedSite = (Site) arr[0];
		}
		comboBox = new JComboBox(arr);
		comboBox.setBounds(10, 11, 184, 20);
		comboBox.addItemListener(this);
		contentPane.setLayout(null);
		contentPane.add(comboBox);
		
		btnInstalliCms = new JButton("Installi CMS");
		btnInstalliCms.setBounds(524, 10, 150, 23);
		contentPane.add(btnInstalliCms);
		
		btnValiInstallikaust = new JButton("Vali installikaust");
		btnValiInstallikaust.setActionCommand("pickInstall");
		btnValiInstallikaust.addActionListener(this);
		btnValiInstallikaust.setBounds(364, 10, 150, 23);
		contentPane.add(btnValiInstallikaust);
		
		btnMuuda = new JButton("Muuda");
		btnMuuda.setBounds(204, 10, 150, 23);
		btnMuuda.setActionCommand("edit");
		btnMuuda.addActionListener(this);
		contentPane.add(btnMuuda);
		
		scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 74, 664, 176);
		contentPane.add(scrollPane);
		
		comboBox_1 = new JComboBox();
		comboBox_1.setBounds(10, 42, 184, 20);
		contentPane.add(comboBox_1);
		
		btnInstalliTemplate = new JButton("Installi Template");
		btnInstalliTemplate.setBounds(204, 40, 150, 23);
		contentPane.add(btnInstalliTemplate);
		
		btnInstalliMoodulid = new JButton("Installi moodulid");
		btnInstalliMoodulid.setBounds(364, 40, 150, 23);
		contentPane.add(btnInstalliMoodulid);
		
		btnUuendaMoodulid = new JButton("Uuenda moodulid");
		btnUuendaMoodulid.setBounds(524, 40, 150, 23);
		contentPane.add(btnUuendaMoodulid);
	}

	public void refresh(){
		SQLiteConnection sqlite = SQLiteConnection.getInstance();
		arr = sqlite.getAllSites().toArray();
		comboBox = new JComboBox(arr);
		comboBox.setBounds(10, 11, 184, 20);
		comboBox.addItemListener(this);
		int index = getSiteIndex();
		selectedSite = (Site) arr[index];
		comboBox.setSelectedIndex(index);
		if (selectedSite.getIsInstalled() == 1) {
			btnInstalliCms.setEnabled(false);
		} else {
			btnInstalliCms.setEnabled(true);
		}
	}
	
	private int getSiteIndex(){
		for(int i = 0; i< arr.length; i++){
			Site s = (Site) arr[i];
			if(s.getId() == selectedSite.getId()){
				return i;
			}
		}
		return -1;
	}
	
	public void itemStateChanged(ItemEvent event) {
		if (event.getStateChange() == ItemEvent.SELECTED) {
			Site item = (Site) event.getItem();
			
			selectedSite = item;
			if (item.getIsInstalled() == 1) {
				btnInstalliCms.setEnabled(false);
			} else {
				btnInstalliCms.setEnabled(true);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		switch(e.getActionCommand()){
		case "edit":
			new AddNewGUI(selectedSite);
			break;
		case "pickInstall":
			SelectFolderDialog dialog = new SelectFolderDialog(this, selectedSite);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
			break;
		}
	}
}
